<template>
  <div class="cost-summary">
    <ProTable
      ref="order_data_summary"
      :immediate="false"
      :request="queryServiceAmountCount"
      :search-form="formList"
      :columns="scencMeta"
      :pagination="false"
      :before-search-submit="beforeSearchSubmit"
    >
      <template #extra>
        <Authc permissions="trade:settlement:report:orderDataSummary:export">
          <Button type="primary" @click="exportExcelAll">下载</Button>
        </Authc>
      </template>
    </ProTable>
  </div>
</template>

<script>
import ProTable from '@/components/ProTable';
import {omit} from 'lodash';
import {serviceAmountCount} from '@/api/statement';
import {getDateYMD,getDateRangeDate} from '@/utils/date';
import Authc from "@/components/Authc";

export default {
  name: 'RevenueCostSummary',
  components: {
    ProTable,
    Authc
  },
  data() {
    return {
      formList: [
        {
          type: 'date',
          name: 'month',
          label: '按月统计',
          value: '',
          props: {
            placeholder: '请选择',
            type: 'month',
            format: 'yyyy年MM月'
          }
        },
        {
          type: 'date',
          name: 'year',
          label: '按年统计',
          value: '',
          props: {
            placeholder: '请选择',
            type: 'year',
            format: 'yyyy年'
          }
        }
      ],
      scencMeta: [
        {
          title: '门票',
          key: 'ticket'
        },
        {
          title: '线路',
          key: 'line'
        },
        {
          title: '酒店',
          key: 'hotel'
        },
        {
          title: '租车',
          key: 'rent_car'
        },
        {
          title: '包车',
          key: 'charterCar'
        },
        {
          title: '机票',
          key: 'plane_ticket'
        },
        {
          title: '汽车票',
          key: 'car_ticket'
        },
        {
          title: 'ETC',
          key: 'etc'
        }
      ]
    };
  },
  computed: {
    columns() {
      const scencColnums = this.scencMeta.map(item => {
        return {...item, align: 'center'};
      });
      let newColnums = [
        {
          title: '',
          key: 'type',
          align: 'center'
        },
        ...scencColnums,
        {
          title: '合计',
          key: 'total',
          align: 'center'
        }
      ];
      return newColnums;
    }
  },
  methods: {
    async queryServiceAmountCount(params) {
      const res = await serviceAmountCount(params);
      if (res && res.data.ret === 0) {
        const list = res.data.data
        if (list && list.length) {
          let newColnums = [{
            title: "",
            key: "category",
            align: 'center'
          }]
          let row=[]
          let service_fail_amount = 0;
          let service_ing_amount = 0;
          let service_success_amount = 0;

          let service_success_row={"category":"服务成功"}
          let service_ing_row={"category":"服务中"}
          let service_fail_row={"category":"服务失败"}
          let total_row={"category":"合计"}
          list.map(item => {
            service_fail_amount += (item.service_fail_amount || 0);
            service_ing_amount += (item.service_ing_amount || 0);
            service_success_amount += (item.service_success_amount || 0);
            item.total_count = (item.service_fail_amount + item.service_ing_amount + item.service_success_amount).toFixed(2);
            service_success_row[item.orderSource.toString()]=item.service_success_amount.toFixed(2)
            service_ing_row[item.orderSource.toString()]=item.service_ing_amount.toFixed(2)
            service_fail_row[item.orderSource.toString()]=item.service_fail_amount.toFixed(2)
            total_row[item.orderSource.toString()]=item.total_count
            newColnums.push({
              title: item.orderSourceName,
              key: item.orderSource,
              align: 'center'
            })
          })
          newColnums.push({
            title: "合计",
            key: "all_source",
            align: 'center'
          })
          this.scencMeta = newColnums

          // 生成行
          service_success_row[`all_source`]=service_success_amount.toFixed(2)
          service_ing_row[`all_source`]=service_ing_amount.toFixed(2)
          service_fail_row[`all_source`]=service_fail_amount.toFixed(2)
          total_row[`all_source`]=(service_success_amount+service_ing_amount+service_fail_amount).toFixed(2)
          row.push(service_success_row,service_ing_row,service_fail_row,total_row)
          res.data.data={
            list:row,
            pagination:{
              "currentPage": 1,
              "pageSize": 20,
              "total": 4,
              "total_page": 1
            }
          }
        }
      }
      console.log(res)
      return res
    },
    exportExcelAll () {
      this.$refs['order_data_summary'].exportLocalCsv("订单汇总数据导出")
    },
    exportExcel() {
    },
    beforeSearchSubmit(data) {
      const {year, month} = data;
      data.year = getDateYMD(year)[0];
      const [monthYear, monthMonth] = getDateYMD(month);
      let newMonthMonth=monthMonth
      if(monthMonth &&monthMonth.toString().length===2){
        data.yearMonth = monthMonth
      }else{
        newMonthMonth = "0"+monthMonth
      }
      data.month =
        monthYear && newMonthMonth ? `${monthYear}-${newMonthMonth}` : null;
      if ((data.year && data.month) || data.year) {
        data.yearMonth = data.year
      } else if (data.month) {
        data.yearMonth = data.month
      } else {
        data.yearMonth = null
      }
      return omit(data, 'date');
    }
  }
};
</script>
